package com.tubang.organization.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.organization.entity.MenuEntity;
import com.tubang.organization.entity.RoleMenuEntity;
import com.tubang.organization.mapper.RoleMenuMapper;
import com.tubang.organization.service.IMenuService;
import com.tubang.organization.service.IRoleMenuService;
import com.tubang.organization.vo.MenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色-菜单 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2017-11-29
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> implements IRoleMenuService {
    @Resource
    private IMenuService menuService;


    @Override
    public List<MenuVo> selectMenuListByRoleId(String id) {

        List<MenuVo> menuVos = new ArrayList<MenuVo>();
        EntityWrapper<RoleMenuEntity> roleMenuWrapper = new EntityWrapper<RoleMenuEntity>();
        roleMenuWrapper.eq("role_id", id);
        List<RoleMenuEntity> roleMenuEntities = selectList(roleMenuWrapper);
        for (RoleMenuEntity roleMenuEntity : roleMenuEntities) {
            MenuVo menuVo = new MenuVo();
            MenuEntity menuEntity = menuService.selectById(roleMenuEntity.getMenuId());
            if (menuEntity == null) continue;
            BeanMapper.copy(menuEntity, menuVo);
            //查询列表的子列表
            List<MenuVo> childLists = selectMenuListByMenuParentId(menuEntity.getId());
            if (childLists != null) {
                menuVo.setChildList(childLists);
            }
            menuVos.add(menuVo);
        }
        return menuVos;
    }

    @Override
    public List<MenuVo> selectMenuListByMenuParentId(String id) {
        List<MenuVo> menuVos = new ArrayList<MenuVo>();
        EntityWrapper<MenuEntity> menuWrapper = new EntityWrapper<MenuEntity>();
        menuWrapper.eq("parent_id", id);
        List<MenuEntity> menuEntities = menuService.selectList(menuWrapper);
        if (menuEntities == null) return null;
        menuVos = BeanMapper.mapList(menuEntities, MenuVo.class);
        return menuVos;
    }

    @Override
    public void deleteByRoleId(String roleId) {
        EntityWrapper<RoleMenuEntity> roleMenuWrapper = new EntityWrapper<RoleMenuEntity>();
        roleMenuWrapper.eq("role_id", roleId);
        List<RoleMenuEntity> roleMenuEntities = selectList(roleMenuWrapper);
        for (RoleMenuEntity roleMenuEntity : roleMenuEntities) {
            roleMenuEntity.setEnable(false);
            insertOrUpdate(roleMenuEntity);
        }

    }

    @Override
    public List<MenuVo> selectRMListByRoleId(String roleId) {
        List<MenuVo> menuVos = new ArrayList<MenuVo>();
        EntityWrapper<RoleMenuEntity> roleMenuWrapper = new EntityWrapper<RoleMenuEntity>();
        roleMenuWrapper.eq("role_id", roleId);
        roleMenuWrapper.eq("menu_lev",Constants.DEFAULT_VAULE_ONE);
        List<RoleMenuEntity> roleMenuEntities = selectList(roleMenuWrapper);
        for (RoleMenuEntity roleMenuEntity : roleMenuEntities) {
            MenuVo menuVo = new MenuVo();
            MenuEntity menuEntity = menuService.selectById(roleMenuEntity.getMenuId());
            BeanMapper.copy(menuEntity, menuVo);
            List<MenuVo> childLists = selectRMenuListByParentId(roleMenuEntity.getId());
            if (childLists == null) continue;
            menuVo.setChildList(childLists);
            menuVos.add(menuVo);
        }
        return menuVos;
    }


    @Override
    public List<MenuVo> selectRMenuListByParentId(String parentId) {
        List<MenuVo> menuVos = new ArrayList<MenuVo>();
        EntityWrapper<RoleMenuEntity> menuWrapper = new EntityWrapper<RoleMenuEntity>();
        menuWrapper.eq("parent_id", parentId);
        List<RoleMenuEntity> roleMenuEntitys = selectList(menuWrapper);
        if (roleMenuEntitys == null) return menuVos;
        for (RoleMenuEntity roleMenuEntity : roleMenuEntitys) {
            MenuVo menuVo = new MenuVo();
            MenuEntity menuEntity = menuService.selectById(roleMenuEntity.getMenuId());
            if (menuEntity == null) continue;
            BeanMapper.copy(menuEntity, menuVo);
            menuVos.add(menuVo);
        }
        return menuVos;
    }

    @Override
    public List<MenuVo> selectRMByRIdHave(String roleId, String userType) {
        List<MenuVo> menuVos = selectRMListByRoleId(userType);
        for (MenuVo menuVo : menuVos) {
            List<MenuVo> menuVosById = selectRMListByRoleId(roleId);
            for (MenuVo voById : menuVosById) {
                if (!menuVo.getId().equals(voById.getId())) continue;
                menuVo.setRoleType(Constants.DEFAULT_VAULE_ONE);
                //判断子列表
                List<MenuVo> childList = menuVo.getChildList();
                for (MenuVo childMenuVo : childList) {
                    List<MenuVo> childListById = voById.getChildList();
                    for (MenuVo voChildById : childListById) {
                        if (!childMenuVo.getId().equals(voChildById.getId())) continue;
                        childMenuVo.setRoleType(Constants.DEFAULT_VAULE_ONE);
                    }
                }
            }

        }
        return menuVos;
    }
}
